package voting;

import commands.Command;

/**
 * A measure represents a proposed change to the state of the game that must be ratified by
 * the players involved in the game.  The measures that are allowed to be voted on in a
 * given game are determined by the GameServer.
 */
public class Measure {
	// The possible necessary numbers of affirmative votes 
	// to declare the vote successful.
	public static final int SIMPLE_MAJORITY = 0;
	public static final int TWO_THIRDS_MAJORITY = 1;
	public static final int UNANIMOUS = 2;

	// The possible necessary quorums to declare the vote successful.
	// A quorom is defined as the number of voters that must cast a vote for the
	// vote to be succesful. 
	public static final int HALF_QUORUM = 0;
	public static final int TWO_THIRDS_QUORUM = 1;
	public static final int ALL_QUORUM = 2;

	// The possible responses to the vote 
	public static final int VOTE_YEA = 1;
	public static final int VOTE_NAY = 0;
	
	public Measure() {
		this(0,0,null,0);
	}

	/**
	 * Construct a new vote with the given parameters.
	 * @param successCriterion the criterion for success for the vote
	 * @param quorum the requisite number of votes cast to effect the vote
	 * @param c a Command that will be executed on each local Game state if the
	 * vote is successful
	 * @param sec the duration of the vote in seconds (after this time, the
	 * final result will be tallied, and if a quorum is not met, the vote will be 
	 * invalid)
	 */
	public Measure(int successCriterion, int quorum, Command c, int sec)
	{
		this.success = successCriterion;
		this.quorum = quorum;
		command = c;
		duration = sec;
	}
	
	
	public Command getCommand() {
		return command;
	}
	public void setCommand(Command command) {
		this.command = command;
	}
	public int getDuration() {
		return duration;
	}
	public void setDuration(int duration) {
		this.duration = duration;
	}
	public int getQuorum() {
		return quorum;
	}
	public void setQuorum(int quorum) {
		this.quorum = quorum;
	}
	public int getSuccess() {
		return success;
	}
	public void setSuccess(int success) {
		this.success = success;
	}

	public String toString() {
		return command.toString() + " [Requires " +
			stringifyQuorum() + " and a " + stringifySuccess() + "]";
	}
	
	private String stringifyQuorum()
	{
		switch (quorum)
		{
			case HALF_QUORUM:
				return "a quorum of half";
			case TWO_THIRDS_QUORUM:
				return "a quorum of two-thirds";
			case ALL_QUORUM:
				return "all players to vote";
			default:
				return "";
		}
	}
	
	private String stringifySuccess()
	{
		switch (success)
		{
			case SIMPLE_MAJORITY:
				return "simple majority";
			case TWO_THIRDS_MAJORITY:
				return "two-thirds majority";
			case UNANIMOUS:
				return "unanimous decision";
			default:
				return "";
		}
	}

	private int success;
	private int quorum;
	private Command command;
	private int duration;
}
